$(function () {
    $("body").on('click',".activationPackage",function () {
        var _this = $(this);
        var courseById = _this.data('id');
        var courseName = _this.data('name');
        activateTheSetMenu(courseById,courseName,_this);
    });

    /**
     * 选择套餐下激活课程
     */
    function activateTheSetMenu(courseBuyId,courseName,actionEm) {
        var _ele = $('#packCoursEle').val();
        layer.open({
            type: 1,
            title: false,
            area: ['880px', '580px'],
            shadeClose: true,
            skin: 'lay_ppComment__wrap',
            content: _ele,
            btn: [' 确定 ', ' 取消 '],
            success: function (layero, index) {
                $.ajax({
                    url:baseloaction+"/web/uc/course/getActivationPackage",
                    data:{"recordId":courseBuyId},
                    dataType:"json",
                    type:"post",
                    success:function (result) {
                        if(result.success==true){
                            $("#packCourseName").html('<span class="fs14"> <i class="fa fa-quote-left"></i> 亲爱的学员，您购买的套餐课程《'+courseName+'》需要进行课程选择：</span>');
                            var _course = result.entity.course;
                            var packList=result.entity.course.packageCourseList;
                            if(_course.useNum==0 || _course.useNum >= packList.length){
                                $("#courseTotalNum").html("最多可选择套餐下全部课程");
                            }else{
                                $("#courseTotalNum").html("最多可选择("+_course.useNum+")门课程");
                            }
                            var package ="";
                            for(var i=0;i<packList.length;i++){
                                if(packList[i].courseId!=0){
                                    if(packList[i].required==1){
                                        package+='<section class="hLh30">'+
                                            '<label class="fs12 c-666"><input class="u-choose vam" type="checkbox" checked disabled  value="'+packList[i].course.id+'">'+packList[i].course.courseName+'</label>'+
                                            '</section>'
                                    }else{
                                        package+='<section class="hLh30">'+
                                            '<label class="fs12 c-666"><input class="u-choose vam" type="checkbox"  value="'+packList[i].course.id+'">'+packList[i].course.courseName+'</label>'+
                                            '</section>'
                                    }
                                }
                            }
                            $('#packageCourseList').html(package);
                        }else{
                            icModale(0, "", result.message, null,null,null);
                            layer.close(index);
                        }
                    }
                });
            },
            yes: function (index, layero) {
                var list = $('#packageCourseList input:checked');
                if(list==null || list.length <=0){
                    icModale(0,'','选择要激活的课程');
                    return;
                }
                var courseIds = list.map(function () {
                    return $(this).val();
                }).get().join(",");

                $.ajax({
                    url:baseloaction+"/web/uc/course/activationPackage",
                    data:{
                        "recordId":courseBuyId,
                        "courseIds":courseIds
                    },
                    dataType:"json",
                    type:"post",
                    success:function (result) {
                        icModale(0, "", result.message);
                        if(result.success==true){
                            //删除点击类
                            actionEm.removeClass('activationPackage');
                            actionEm.removeAttr('data-id');
                            actionEm.removeAttr('data-name');
                            //添加类
                            actionEm.addClass('startLearning');
                            actionEm.text('开始学习');
                            actionEm.parent().attr('href',baseloaction + '/web/uc/course/info?recordId='+courseBuyId);
                            layer.close(index);
                        }
                    }
                });
            }
        });
    }
});